//@author A0101200R

package testcases;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import storage.ScheduleEvent;
import storage.Storage;

public class StorageTestAddDelete {
	
	// list of testcases
	private static final String TESTCASE_FLOATING = "floating testcase";
	private static final String TESTCASE_TIMEDTASK = "timedtask testcase";
	private static final String TESTCASE_DEADLINE2 = "deadline2 testcase";
	private static final String TESTCASE_DEADLINE1 = "deadline1 testcase";
	
	// list of constants
	private static final int EVENT_TYPE_FLOATING = 0;
	private static final int EVENT_TYPE_DEADLINE = 1;
	private static final int EVENT_TYPE_TIMED = 2;

	// list of success messages
	private static final String SUCCESS_ALL_EVENTS_CLEARED = "All events have been cleared from your Schedule!";
	private static final String SUCCESS_DELETED_EVENT = "The event \"%s\" has been removed successfully from your Schedule!";
	private static final String SUCCESS_ADDED_EVENT = "The event \"%s\" has been added successfully to your Schedule!";

	// list of error messages
	private static final String ERROR_MSG_INVALID_INDEX = "ERROR! An invalid Index was input. Unable to perform Delete. Please try again!";

	// list of variables from other classes
	Storage storageTest = new Storage("StorageAddDelTest.txt");
	ScheduleEvent event;

	private String formatString(String msg, String input) {
		return String.format(msg, input);
	}

	private String addDeadline(int eventType, String taskname, String date,
			String endTime) {
		String output;

		if (endTime == null) {
			event = new ScheduleEvent(eventType, taskname, date);
			output = storageTest.addEvent(event);
		} else {
			event = new ScheduleEvent(eventType, taskname, date, endTime);
			output = storageTest.addEvent(event);
		}
		return output;
	}
	
	private String addTimed(int eventType, String taskname, String date, String startTime,
			String endTime) {
		String output;
		
		event = new ScheduleEvent(eventType, taskname, date, startTime, endTime);
		output = storageTest.addEvent(event);
		
		return output;
	}
	
	private String addFloat(int eventType, String taskname, String date) {
		String output;
		
		event = new ScheduleEvent(eventType, taskname, date);
		output = storageTest.addEvent(event);
		
		return output;
	}
	
	private String delete(int index) {
		return storageTest.deleteEvent(index);
	}

	@Test
	public void test() {
		String actual, expected;

		actual = storageTest.clearAllEvents();
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED, actual);
		
		expected = formatString(SUCCESS_ADDED_EVENT, TESTCASE_DEADLINE1);
		actual = addDeadline(EVENT_TYPE_FLOATING, TESTCASE_DEADLINE1, "01052014", null);
		assertEquals(expected, actual);
		
		expected = formatString(SUCCESS_ADDED_EVENT, TESTCASE_DEADLINE2);
		actual = addDeadline(EVENT_TYPE_DEADLINE, TESTCASE_DEADLINE2, "01052014", "1700");
		assertEquals(expected, actual);
		
		expected = formatString(SUCCESS_ADDED_EVENT, TESTCASE_TIMEDTASK);
		actual = addTimed(EVENT_TYPE_TIMED, TESTCASE_TIMEDTASK, "02052014", "1200", "1700");
		assertEquals(expected, actual);
		
		expected = formatString(SUCCESS_ADDED_EVENT, TESTCASE_FLOATING);
		actual = addFloat(EVENT_TYPE_FLOATING, TESTCASE_FLOATING, "99999999");
		assertEquals(expected, actual);

		expected = formatString(SUCCESS_DELETED_EVENT, TESTCASE_DEADLINE1);
		actual = delete(1);
		assertEquals(expected, actual);
		
		actual = delete(5);
		assertEquals(ERROR_MSG_INVALID_INDEX, actual);
		
		storageTest.saveEvents();
	}
}
